package com.bookpavilion.file.controller;

import com.bookpavilion.file.service.FileService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file){
        Map<String, Object> map = fileService.upload(file);
        if ((boolean)map.get("isLegal"))
        /**
         * Map:  isLegal:合法标志
         *       fileURL：文件地址
         */
            return new Result(true, StatusCode.OK,"文件上传成功",map);
        return new Result(false, StatusCode.ERROR,"文件上传失败");

    }

    @DeleteMapping("/detele")
    public Result delete(@RequestBody Map<String,String> map){
        boolean isSuccessful = fileService.delete(map.get("filePath"));
        if (isSuccessful)
            return new Result(true, StatusCode.OK,"文件删除成功");
        return new Result(true, StatusCode.ERROR,"文件删除失败");
    }
}
